// $Id: param_info_list.c,v 1.1.1.1 2012/02/17 03:45:26 hzheng Exp $

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "log.h"

#include "data.h"

void evcpe_param_info_list_init(struct evcpe_param_info_list *list)
{
	TAILQ_INIT(&list->head);
	list->size = 0;
}

void evcpe_param_info_list_clear(struct evcpe_param_info_list *list)
{
	struct evcpe_param_info *param;

	evcpe_debug(__func__, "clearing evcpe_param_info_list");

	while((param = TAILQ_FIRST(&list->head))) {
		TAILQ_REMOVE(&list->head, param, entry);
		if (param->content)
		{
			free(param->content);
			param->content = NULL;
		}
		free(param);
	}
	list->size = 0;
}

unsigned int evcpe_param_info_list_size(struct evcpe_param_info_list *list)
{
	return list->size;
}

int evcpe_param_info_list_add(struct evcpe_param_info_list *list,
		struct evcpe_param_info **param, const char *name, unsigned len,
		int writable)
{
	if (!name || !len) return EINVAL;
	if (len >= sizeof((*param)->name)) return EOVERFLOW;

	//evcpe_debug(__func__, "adding param name: %.*s", len, name);

	if (!((*param) = calloc(1, sizeof(struct evcpe_param_info))))
		return ENOMEM;

	strncpy((*param)->name, name, len);
	(*param)->writable = writable;
	TAILQ_INSERT_TAIL(&list->head, (*param), entry);
	list->size ++;
	return 0;
}

void evcpe_param_info_list_remove(struct evcpe_param_info_list *list,
		struct evcpe_param_info *param)
{
	TAILQ_REMOVE(&list->head, param, entry);
	list->size --;
	free(param);
}
void evcpe_fault_detail_list_init(struct evcpe_fault_detail_list *list)
{
	TAILQ_INIT(&list->head);
	list->size = 0;
}

void evcpe_fault_detail_list_clear(struct evcpe_fault_detail_list *list)
{
	struct evcpe_fault_detail *param;

	evcpe_debug(__func__, "clearing evcpe_fault_detail_list");

	while((param = TAILQ_FIRST(&list->head))) {
		TAILQ_REMOVE(&list->head, param, entry);
		
		free(param);
	}
	list->size = 0;
}

unsigned int evcpe_fault_detail_list_size(struct evcpe_fault_detail_list *list)
{
	return list->size;
}

int evcpe_fault_detail_list_add(struct evcpe_fault_detail_list *list,
		int code,char *node)//enum evcpe_fault_code code
{
	struct evcpe_fault_detail *new_fault = NULL;
	if((NULL == list) || (NULL == node))
		 return EINVAL;

	evcpe_info(__func__, "adding param fault detail: %d %s", code, node);
	new_fault = calloc(1,sizeof(struct evcpe_fault_detail));

	if (!new_fault)
		return ENOMEM;

	new_fault->code = code; 	
	memcpy(new_fault->string, 
		evcpe_fault_code_to_str(new_fault->code), sizeof(new_fault->string));
	strcpy(new_fault->fault_param,node);

	TAILQ_INSERT_TAIL(&list->head, (new_fault), entry);
	list->size ++;
	return 0;
}
